
export function isEqualDiffWithBinaryTree (root: BinaryNode) {

    const result: number[] = [];

    inOrder(root, (node: BinaryNode) => {
        result.push(node.val);
    });

    return isDiffQueue(result);

}

function isDiffQueue(arr: number[]) {
    if (arr.length < 3) return true;

    const diff = arr[1] - arr[0];

    for(let i = 2; i < arr.length; i++) {
        const tempDiff = arr[i] - arr[i-1];
        if (tempDiff !== diff) return false;
    }

    return true;
}

function inOrder (node: BinaryNode, callBack: Function) {
    if (!node) return;

    if (!node.left && !node.right) { // leaf node
        callBack(node);
        return;
    }

    inOrder(node.left, callBack);
    inOrder(node.right, callBack);
}

interface BinaryNode {
    val: number,
    left: BinaryNode,
    right: BinaryNode
}